//
//  NewFeatureViewController.swift
//  Weibo10
//
//  Created by Leon on 15/10/17.
//  Copyright © 2015年 Leon. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"
private let NewFeaturePageNum = 4

class NewFeatureViewController: UICollectionViewController {
    
    // MREK: - 构造函数
    init() {
        
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = .Horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }


    // MARK: - UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return NewFeaturePageNum
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> NewFeatureCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
        cell.imageIndex = indexPath.item
    
        return cell
    }

    // MARK: UICollectionViewDelegate
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        // 取得当前页
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        // 判断是否是最后一页
        if page != NewFeaturePageNum - 1 {
            return
        }
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! NewFeatureCell
        cell.openNewFeatureAnim()
        
    }

}

class NewFeatureCell: UICollectionViewCell {
    
    // 图片索引
    var imageIndex: Int = 0 {
        didSet {
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            // 隐藏按钮
            openNewFeature.hidden = true
        }
    }
    
    /// openNewFeature 的动画
    private func openNewFeatureAnim() {
        openNewFeature.hidden = false
        openNewFeature.userInteractionEnabled = false
        openNewFeature.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1.0,
            delay: 0,
            usingSpringWithDamping: 0.7,
            initialSpringVelocity: 10,
            options: [],
            animations: { () -> Void in
                
                self.openNewFeature.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                self.openNewFeature.userInteractionEnabled = true
        }
    }
    
    // openNewFeature 点击事件
    @objc private func openNewFeatureClick() {
        
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchViewControllerNotification, object: nil)
    }
    
    // MARK: - 构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        // 0 不能单纯在此，重用问题
        openNewFeature.hidden = true
        // 1 添加到cell
        contentView.addSubview(iconView)
        contentView.addSubview(openNewFeature)
        
        // 2. 自动布局 － 如果大小是固定的，而且能够获取到的，类似于全屏，可以直接设置 frame
        iconView.frame = bounds
        
        openNewFeature.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(snp_centerX)
            make.bottom.equalTo(snp_bottom).multipliedBy(0.7)
            }
        // 3 监听方法
        openNewFeature.addTarget(self, action: "openNewFeatureClick", forControlEvents: .TouchUpInside)
    }
    
    private var iconView = UIImageView()
    
    private var openNewFeature = UIButton(title: "立即开启", titleColor: UIColor.whiteColor(), imageName: "new_feature_finish_button")
    
}




